<%--
    Document   : admin
    Created on : 19-Feb-2011, 21:09:24
    Author     : Prabhat
    Description: This page is used to monitor the users and forums on the chat
                 service. It allows 1) registering/deleting users and 2) adding/
                 deleting forums.
--%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="net.chat.User"%>
<%@page import="net.chat.Forum"%>
<%@page import="net.chat.server.ChatServer"%>
<%@page import="net.chat.utils.Util"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
        <title>Net-Chat:Admin</title>
        <link rel="stylesheet" type="text/css" href="../style/common.css"></link>
        <style type="text/css">
            td, th
            {
                padding:2px;
            }
            .pad {
                padding: 5px 5px 5px 5px;
            }
        </style>
    </head>

    <body>
        <div id="container">
            <div id="top">
                <h1>net-chat</h1>
            </div>
            <div id="a" class="pad">
                <h2>Admin</h2>
                This page is intended for testing purposes, to monitor and manage the users and forums in the chat service. <i>No security consideration was made. It can be accessed by anyone but this should be restricted in real life.</i>
                <br/>
                <br/>Server status: Running
                <br/>HOST_URL: <%= Util.HOST_URL%>
                <br/>WEBAPP_URL: <%= Util.WEBAPP_URL%>
                <br/>SERVLET_PATH: <%= Util.SERVLET_PATH%>

            </div>
            <%
                        ChatServer server = ChatServer.getInstance();
            %>

            <div id="u" class="pad">

                <h3>Users</h3><hr/>
                <strong>Authorised</strong>
                <table border="1">
                    <tr>
                        <th>uid</th>
                        <th>fname</th>
                        <th>lname</th>
                        <th>&nbsp;</th>
                    </tr>
                    <%
                                List<User> users = server.getUsers();
                                if (users.size() > 0) {
                                    for (Iterator<User> itr = users.iterator(); itr.hasNext();) {
                                        User user = itr.next();
                                        out.println("<tr>");
                                        out.println("<td>" + user.getUid() + "</td>");
                                        out.println("<td>" + user.getFname() + "</td>");
                                        out.println("<td>" + user.getLname() + "</td>");
                                        if (user.getUid() == 1) {
                                            out.println("<td>admin</td>");
                                        } else {
                                            out.println("<td><a href='" + Util.SERVLET_PATH + "UserServlet?action=delete&from=admin&uid=" + user.getUid() + "'>delete</a></td>");
                                        }
                                        out.println("</tr>");
                                    }
                                } else {
                                    out.println("<tr><td colspan=\"4\">No users</td></tr>");
                                }
                    %>
                </table>

                <br/>
                <form action="<%= Util.SERVLET_PATH%>UserServlet" method="post">
                    <div>
                        <strong>Register</strong>
                        <%
                                    String err = request.getParameter("err");
                                    String uid = request.getParameter("uid");
                                    if (err != null && err.equals("u")) {
                                        out.println("<br/><span class=\"err\">All fields are mandatory</span>");
                                    }
                                    if (uid != null) {
                                        out.println("<br/><span class=\"suc\">Registration successful. Your User ID is " + uid + ". Login to chat!</span>");
                                    }
                        %>
                        <p>
                            <label class="w120" for="fname">fname: </label>
                            <input type="text" name="fname" id="fname" />
                            <br />
                            <label class="w120" for="lname">lname: </label>
                            <input type="text" name="lname" id="lname" />
                            <br />
                            <label class="w120" for="pwd">pwd: </label>
                            <input type="text" name="pwd" id="pwd" />
                        </p>
                        <p>
                            <input type="hidden" name="action" value="register" />
                            <input type="hidden" name="from" value="admin" />
                            <input type="submit" value="submit" />
                        </p></div>
                </form>
            </div>
            <div id="f" class="pad">
                <h3>Forums</h3><hr/>
                <strong>Active</strong>
                <table border="1">
                    <tr>
                        <th>fid</th>
                        <th>name</th>
                        <th>category</th>
                        <th>created</th>
                        <th>users</th>
                        <th>messages</th>
                        <th>&nbsp;</th>
                    </tr>
                    <%
                                List<Forum> forums = server.getForums();
                                if (forums.size() > 0) {
                                    for (Iterator<Forum> itr = forums.iterator(); itr.hasNext();) {
                                        Forum forum = itr.next();
                                        out.println("<tr>");
                                        out.println("<td>" + forum.getFid() + "</td>");
                                        out.println("<td>" + forum.getName() + "</td>");
                                        out.println("<td>" + forum.getCategory() + "</td>");
                                        out.println("<td>" + forum.getCreated() + "</td>");
                                        out.println("<td>" + forum.getUsers().size() + "</td>");
                                        out.println("<td>" + forum.getOldMessages().size() + "/" + forum.getNewMessages().size() + "</td>");
                                        out.println("<td><a href='" + Util.SERVLET_PATH + "ForumServlet?action=delete&from=admin&fid=" + forum.getFid() + "'>delete</a></td>");
                                        out.println("</tr>");
                                    }
                                } else {
                                    out.println("<tr><td colspan=\"7\">No forums</td></tr>");
                                }
                    %>
                </table>

                <br/>
                <form action="<%= Util.SERVLET_PATH%>ForumServlet" method="post">
                    <div>
                        <strong>Create new</strong>
                        <%
                                    String fid = request.getParameter("fid");
                                    if (err != null && err.equals("f")) {
                                        out.println("<br/><span class=\"err\">All fields are mandatory</span>");
                                    }
                                    if (fid != null) {
                                        out.println("<br/><span class=\"suc\">New forum added. Forum ID is " + fid + ". </span>");
                                    }
                        %>

                        <p>
                            <label class="w120" for="fname">name: </label>
                            <input type="text" name="fname" id="name" />
                            <br />
                            <label class="w120" for="fcategory">category: </label>
                            <input type="text" name="fcategory" id="fcategory" />
                        </p>
                        <p>
                            <input type="hidden" name="action" value="create" />
                            <input type="hidden" name="from" value="admin" />
                            <input type="submit" value="submit" />
                        </p></div>
                </form>
            </div>

            <div id="footer">
                <div class="alignleft">Author: Totoo</div>
                <div class="alignright">Network Applications</div>
                <div class="clr"></div>
            </div>
        </div>
        <p class="center">
            <a href="http://validator.w3.org/check?uri=referer">
                <img class="w3img" src="../img/valid-xhtml10-blue.png" alt="Valid XHTML 1.0 Strict" />
            </a>
            <a href="http://jigsaw.w3.org/css-validator/check/referer">
                <img class="w3img" src="../img/vcss-blue.gif" alt="Valid CSS!" />
            </a>
        </p>
    </body>
</html>